package com.xb.loan.admin.service.rolemanager.impl;

import com.xb.loan.admin.bean.system.OssRole;
import com.xb.loan.admin.bean.system.OssRoleMenu;
import com.xb.loan.admin.dao.system.OssOperatorRoleDao;
import com.xb.loan.admin.dao.system.OssRoleDao;
import com.xb.loan.admin.dao.system.OssRoleMenuDao;
import com.xb.loan.admin.service.dict.IenumConstants;
import com.xb.loan.admin.service.rolemanager.RoleManagerService;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.StringUtils;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.mybatis.MyTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("roleManagerService")
public class RoleManagerServiceImpl implements RoleManagerService {
	
	@Autowired
	private OssRoleDao ossRoleDao;
	
	@Autowired
	private OssRoleMenuDao ossRoleMenuDao;
	
	@Autowired
	private OssOperatorRoleDao ossOperatorRoleDao;
	
	private final static String OPERA_ERROR = "操作失败!";
	private final static String ROLE_EXIST = "角色已存在!";
	private final static String PARAM_ERROR = "参数错误!";
	
	private int res = 0;

	@Override
	@MyTransactional
	public String saveOrUpdateRole(String type, String roleId, String roleName,
			String[] partnerIdArray, String[] menuIdArray,String parentRoleId)
			throws BizException {
		OssRole ossRole = ossRoleDao.selectByRoleName(roleName);
		if ("save".equals(type)) {
			if (ossRole!=null) {
				throw new BizException("",ROLE_EXIST);
			}
		} else if ("update".equals(type)){
			if (ossRole!=null && !ossRole.getRoleId().equals(roleId)) {
				throw new BizException("",ROLE_EXIST);
			}
			res = ossRoleMenuDao.deleteByPrimaryKey(roleId);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
			OssRole or = new OssRole();
			or.setRoleId(roleId);
			or.setRoleName(roleName);
			res = ossRoleDao.updateByPrimaryKey(or);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
		}else {
			throw new BizException("",PARAM_ERROR);
		}
		return this.saveRole(roleId,roleName, partnerIdArray, menuIdArray, parentRoleId);
	}
	
	@MyTransactional
	public String saveRole(String roleId,String roleName,String[] partnerIdArray, String[] menuIdArray,String parentRoleId) throws BizException{
		if (StringUtils.isEmpty(roleId)) {
			roleId = FormatOperation.assemblySeq(
					IenumConstants.PriKeyStrategy.OSS_ROLE.getCode(),
					IenumConstants.PriKeyStrategy.OSS_ROLE.getNum(),
					FormatOperation.formatDateToYMDHms(new Date()), "");
			OssRole or = new OssRole();
			or.setRoleId(roleId);
			or.setRoleName(roleName);
			or.setParentRoleId(parentRoleId);
			res = ossRoleDao.insert(or);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
		}

		OssRoleMenu orm = new OssRoleMenu();
		orm.setRoleId(roleId);
		for (String menuId : menuIdArray) {
			orm.setMenuId(menuId);
			res = ossRoleMenuDao.insert(orm);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
		}
		return roleId;
	}

	@Override
	public List<OssRole> queryOssRoleByPage(String parentRoleId,int pageSize, int pageNum) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("parentRoleId", parentRoleId);
		return ossRoleDao.queryOssRoleByPage(map);
	}

	@Override
	public int queryOssRoleCount(String parentRoleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentRoleId", parentRoleId);
		return ossRoleDao.queryOssRoleCount(map);
	}

	@Override
	@MyTransactional
	public void deleteByRoleId(String roleId) throws BizException{
		res = ossRoleMenuDao.deleteByPrimaryKey(roleId);
		if (res<1) {
			throw new BizException("",OPERA_ERROR);
		}
		res = ossRoleDao.deleteByPrimaryKey(roleId);
		if (res<1) {
			throw new BizException("",OPERA_ERROR);
		}
	}


	@Override
	public List<String> queryMenuIdListByRoleId(String roleId) {
		return ossRoleMenuDao.queryMenuIdListByRoleId(roleId);
	}

  


    @Override
    public List<String> queryMenuIdListByOperatorId(String operatorId) {
        return ossRoleMenuDao.queryMenuIdListByOperatorId(operatorId);
    }

	@Override
	public int queryOperatorRoleCountByRoleId(String roleId) {
		return ossOperatorRoleDao.queryOperatorRoleCountByRoleId(roleId);
	}
	
}
